package pers.jl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import pers.jl.common.MyException;
import pers.jl.entity.AddressBook;
import pers.jl.mapper.AddressBookMapper;
import pers.jl.service.IAddressBookService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * <p>
 * 地址管理 服务实现类
 * </p>
 *
 * @author luge
 * @since 2023-04-22
 */
@Service
public class AddressBookServiceImpl extends ServiceImpl<AddressBookMapper, AddressBook> implements IAddressBookService {

    /**
     * 用户新增地址
     * @param address
     * @return
     */
    @Override
    public void saveAddress(HttpSession session,AddressBook address) {
        // 给地址簿设置用户id
        address.setUserId((Long) session.getAttribute("userId"));
        this.save(address);
    }


    /**
     * 查询当前用户所有地址信息
     * @param session
     * @return
     */
    @Override
    public List<AddressBook> getAllAddress(HttpSession session) {

        // 根据用户id，查出所有地址信息
        LambdaQueryWrapper<AddressBook> addressBookLambdaQueryWrapper = new LambdaQueryWrapper<>();
        addressBookLambdaQueryWrapper.eq(AddressBook::getUserId,session.getAttribute("userId"));
        // 执行查询
        List<AddressBook> addressBookList = this.list(addressBookLambdaQueryWrapper);
        return addressBookList;
    }

    /**
     * 修改地址簿
     * @param address
     */
    @Override
    public void updateAddress(AddressBook address) {
        // 直接修改
        this.updateById(address);
    }

    /**
     * 修改地址簿时，数据回显
     * @param id
     * @return
     */
    @Override
    public AddressBook getByAddressId(Long id) {
        // 根据地址id，查出地址对象，进行数据回显
        return this.getById(id);
    }

    /**
     * 设置为默认地址
     * @param address
     * @return
     */
    @Override
    public void change(AddressBook address,HttpSession session) {
        // 1. 将该用户所有未删除地址设为非默认地址
        LambdaUpdateWrapper<AddressBook> addressBookLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        // 指定用户
        addressBookLambdaUpdateWrapper.eq(AddressBook::getUserId,session.getAttribute("userId"));
        // 全部修改为非默认地址
        addressBookLambdaUpdateWrapper.set(AddressBook::getIsDefault,0);
        // 执行查询
        this.update(addressBookLambdaUpdateWrapper);

        // 2. 根据地址id查出地址对象
        LambdaQueryWrapper<AddressBook> addressBookLambdaQueryWrapper = new LambdaQueryWrapper<>();
        addressBookLambdaQueryWrapper.eq(AddressBook::getId,address.getId());
        AddressBook one = this.getOne(addressBookLambdaQueryWrapper);

        // 3. 将地址对象设为默认地址,然后更新
        one.setIsDefault(1);
        this.updateById(one);
    }

    /**
     * 删除地址
     * @param ids
     */
    @Override
    public void deleteAddressBook(String ids) {
        // 判断是否是默认地址
        LambdaQueryWrapper<AddressBook> addressBookLambdaQueryWrapper = new LambdaQueryWrapper<>();
        addressBookLambdaQueryWrapper.eq(AddressBook::getId,ids);
        AddressBook one = this.getOne(addressBookLambdaQueryWrapper);
        Integer isDefault = one.getIsDefault();
        if (isDefault == 1) {
            // 如果是默认地址，不能删除
            throw new MyException("默认地址不能删除！");
        }else {
            // 如果不是默认地址，直接根据地址id删除地址
            this.removeById(ids);
        }
    }

    /**
     * 获取默认地址
     * @return
     */
    @Override
    public AddressBook getDefaultAddress() {
        LambdaQueryWrapper<AddressBook> addressBookLambdaQueryWrapper = new LambdaQueryWrapper<>();
        addressBookLambdaQueryWrapper.eq(AddressBook::getIsDefault,1);
        // 查询默认地址(isDefault等于1)
        AddressBook addressBook = this.getOne(addressBookLambdaQueryWrapper);
        return addressBook;
    }
}
